/*
 * Copyright (c) 2017. All  rights reserved.
 * 项目名：microservice-base
 * 文件名：CustomAuthenticationProvider.java
 * Date  ：17-11-16 下午4:51
 * Author：abin
 *
 */

package com.stadium.common.config.security;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/11/14.
 */
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		// 获取认证的用户名 & 密码
		Authentication auth = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
				authentication.getCredentials());
 		SecurityContextHolder.getContext().setAuthentication(authentication);
		return auth;
	}

	// 是否可以提供输入类型的认证服务
	@Override
	public boolean supports(Class<?> authentication) {
		return authentication.equals(UsernamePasswordAuthenticationToken.class);
	}
}
